using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Media;
using Common;
using Common.Enum;
using Common.NotifyEvent;
using SProject.Paramter;

namespace SProject.Rule
{
	public class RuleItem : NotifyPropertyChanged
	{
		private SampleType _SampleType;

		private bool _IsAnyTarget;

		private string _TargetName;

		private BasicOptionItem _BasicOptionItem;

		private double _LeftValue = double.NaN;

		private EOperator _LeftOperator;

		private ECalcResult _CalcResult;

		private EOperator _RightOperator;

		private double _RightValue = double.NaN;

		private string _Content;

		private Color _Color = Colors.Black;

		private ObservableCollection<KeyValuePair> _AllCalcResult = new ObservableCollection<KeyValuePair>();

		private ObservableCollection<KeyValuePair> _AllLeftOperator = new ObservableCollection<KeyValuePair>
		{
			new KeyValuePair
			{
				Key = EOperator.Null,
				Value = ""
			},
			new KeyValuePair
			{
				Key = EOperator.LessThan,
				Value = Utility.GetMessage("operator_less")
			},
			new KeyValuePair
			{
				Key = EOperator.EqualLessThan,
				Value = Utility.GetMessage("operator_less_equal")
			}
		};

		private ObservableCollection<KeyValuePair> _AllRightOperator = new ObservableCollection<KeyValuePair>
		{
			new KeyValuePair
			{
				Key = EOperator.Null,
				Value = ""
			},
			new KeyValuePair
			{
				Key = EOperator.LessThan,
				Value = Utility.GetMessage("operator_less")
			},
			new KeyValuePair
			{
				Key = EOperator.EqualLessThan,
				Value = Utility.GetMessage("operator_less_equal")
			},
			new KeyValuePair
			{
				Key = EOperator.Equal,
				Value = Utility.GetMessage("operator_equal")
			},
			new KeyValuePair
			{
				Key = EOperator.UnEqual,
				Value = Utility.GetMessage("operator_unequal")
			}
		};

		public SampleType SampleType
		{
			get
			{
				return _SampleType;
			}
			set
			{
				if (_SampleType != value)
				{
					_SampleType = value;
					UpdateAllCalcResult();
					OnPropertyChanged("SampleType");
				}
			}
		}

		public bool IsAnyTarget
		{
			get
			{
				return _IsAnyTarget;
			}
			set
			{
				if (_IsAnyTarget != value)
				{
					_IsAnyTarget = value;
					OnPropertyChanged("IsAnyTarget");
				}
			}
		}

		public string TargetName
		{
			get
			{
				return _TargetName;
			}
			set
			{
				if (!(_TargetName == value))
				{
					_TargetName = value;
					OnPropertyChanged("TargetName");
				}
			}
		}

		public BasicOptionItem BasicOptionItem
		{
			get
			{
				return _BasicOptionItem;
			}
			set
			{
				if (_BasicOptionItem != value)
				{
					if (value != null)
					{
						TargetName = value.TargetName;
						value.Event += BasicOptionItem_Event;
					}
					if (_BasicOptionItem != null)
					{
						_BasicOptionItem.Event -= BasicOptionItem_Event;
					}
					_BasicOptionItem = value;
					UpdateAllCalcResult();
					OnPropertyChanged("BasicOptionItem");
				}
			}
		}

		public double LeftValue
		{
			get
			{
				return _LeftValue;
			}
			set
			{
				if (_LeftValue != value)
				{
					_LeftValue = value;
					OnPropertyChanged("LeftValue");
				}
			}
		}

		public EOperator LeftOperator
		{
			get
			{
				return _LeftOperator;
			}
			set
			{
				if (_LeftOperator != value)
				{
					_LeftOperator = value;
					OnPropertyChanged("LeftOperator");
				}
			}
		}

		public ECalcResult CalcResult
		{
			get
			{
				return _CalcResult;
			}
			set
			{
				if (_CalcResult != value)
				{
					_CalcResult = value;
					OnPropertyChanged("CalcResult");
				}
			}
		}

		public EOperator RightOperator
		{
			get
			{
				return _RightOperator;
			}
			set
			{
				if (_RightOperator != value)
				{
					_RightOperator = value;
					OnPropertyChanged("RightOperator");
				}
			}
		}

		public double RightValue
		{
			get
			{
				return _RightValue;
			}
			set
			{
				if (_RightValue != value)
				{
					_RightValue = value;
					OnPropertyChanged("RightValue");
				}
			}
		}

		public string Content
		{
			get
			{
				return _Content;
			}
			set
			{
				if (!(_Content == value))
				{
					_Content = value;
					OnPropertyChanged("Content");
				}
			}
		}

		public Color Color
		{
			get
			{
				return _Color;
			}
			set
			{
				if (!(_Color == value))
				{
					_Color = value;
					OnPropertyChanged("Color");
				}
			}
		}

		public ObservableCollection<KeyValuePair> AllCalcResult
		{
			get
			{
				return _AllCalcResult;
			}
		}

		public ObservableCollection<KeyValuePair> AllLeftOperator
		{
			get
			{
				return _AllLeftOperator;
			}
		}

		public ObservableCollection<KeyValuePair> AllRightOperator
		{
			get
			{
				return _AllRightOperator;
			}
		}

		private void BasicOptionItem_Event(object sender, EventArgs e)
		{
			PropertyChangedEventArgsEx propertyChangedEventArgsEx = e as PropertyChangedEventArgsEx;
			if (propertyChangedEventArgsEx != null)
			{
				if (propertyChangedEventArgsEx.PropertyName == "TargetName")
				{
					TargetName = ((BasicOptionItem)sender).TargetName;
				}
				else if (propertyChangedEventArgsEx.PropertyName == "AnalysisType")
				{
					UpdateAllCalcResult();
				}
			}
		}

		private void UpdateAllCalcResult()
		{
			AllCalcResult.Clear();
			if (BasicOptionItem == null)
			{
				return;
			}
			List<KeyValuePair> allCalcResult = EnumManagement.GetAllCalcResult();
			if (SampleType == SampleType.Standard)
			{
				AllCalcResult.Add(allCalcResult[3]);
				CalcResult = ECalcResult.r;
				return;
			}
			AllCalcResult.Add(allCalcResult[0]);
			AllCalcResult.Add(allCalcResult[2]);
			AllCalcResult.Add(allCalcResult[1]);
			if (CalcResult == ECalcResult.r || CalcResult == ECalcResult.None)
			{
				CalcResult = ECalcResult.Ct;
			}
		}

		public RuleItem Clone()
		{
			RuleItem ruleItem = new RuleItem();
			ruleItem.SampleType = SampleType;
			ruleItem.IsAnyTarget = IsAnyTarget;
			ruleItem.TargetName = TargetName;
			ruleItem.BasicOptionItem = BasicOptionItem;
			ruleItem.LeftValue = LeftValue;
			ruleItem.LeftOperator = LeftOperator;
			ruleItem.CalcResult = CalcResult;
			ruleItem.RightOperator = RightOperator;
			ruleItem.RightValue = RightValue;
			ruleItem.Content = Content;
			ruleItem.Color = Color;
			return ruleItem;
		}

		public void SaveToStream(StreamEx stream)
		{
			stream.Write(PropertyKey.Key_RuleItem);
			List<PropertyKey> list = new List<PropertyKey>();
			List<byte[]> list2 = new List<byte[]>();
			list.Add(PropertyKey.Key_SampleType);
			list2.Add(BytesConverter.GetBytes((int)SampleType));
			if (!string.IsNullOrEmpty(TargetName))
			{
				list.Add(PropertyKey.Key_TargetName);
				list2.Add(BytesConverter.GetBytes(TargetName));
			}
			list.Add(PropertyKey.Key_LeftValue);
			list2.Add(BytesConverter.GetBytes(LeftValue));
			list.Add(PropertyKey.Key_LeftOperator);
			list2.Add(BytesConverter.GetBytes((int)LeftOperator));
			list.Add(PropertyKey.Key_CalcResult);
			list2.Add(BytesConverter.GetBytes((int)CalcResult));
			list.Add(PropertyKey.Key_RightOperator);
			list2.Add(BytesConverter.GetBytes((int)RightOperator));
			list.Add(PropertyKey.Key_RightValue);
			list2.Add(BytesConverter.GetBytes(RightValue));
			if (!string.IsNullOrEmpty(Content))
			{
				list.Add(PropertyKey.Key_Content);
				list2.Add(BytesConverter.GetBytes(Content));
			}
			list.Add(PropertyKey.Key_Color);
			list2.Add(BytesConverter.GetBytes(Color.ToString()));
			list.Add(PropertyKey.Key_IsAnyTarget);
			list2.Add(BytesConverter.GetBytes(IsAnyTarget));
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(list, list2);
			stream.Flush();
		}

		public bool ReadFromStream(StreamEx stream)
		{
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_RuleItem)
			{
				propertyKey = stream.ReadKey();
				if (propertyKey == PropertyKey.Key_Index)
				{
					List<PropertyKey> keys;
					List<byte[]> dates;
					stream.ReadIndex(out keys, out dates);
					for (int i = 0; i < keys.Count; i++)
					{
						switch (keys[i])
						{
						case PropertyKey.Key_SampleType:
							SampleType = (SampleType)BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_TargetName:
							TargetName = BytesConverter.BytesToString(dates[i]);
							break;
						case PropertyKey.Key_LeftValue:
							LeftValue = BytesConverter.BytesToDouble(dates[i]);
							break;
						case PropertyKey.Key_LeftOperator:
							LeftOperator = (EOperator)BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_CalcResult:
							CalcResult = (ECalcResult)BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_RightOperator:
							RightOperator = (EOperator)BytesConverter.BytesToInt(dates[i]);
							break;
						case PropertyKey.Key_RightValue:
							RightValue = BytesConverter.BytesToDouble(dates[i]);
							break;
						case PropertyKey.Key_Content:
							Content = BytesConverter.BytesToString(dates[i]);
							break;
						case PropertyKey.Key_Color:
							Color = (Color)ColorConverter.ConvertFromString(BytesConverter.BytesToString(dates[i]));
							break;
						case PropertyKey.Key_IsAnyTarget:
							IsAnyTarget = BytesConverter.BytesToBool(dates[i]);
							break;
						}
					}
					return true;
				}
				stream.Seek(-4L, SeekOrigin.Current);
				while (true)
				{
					switch (stream.ReadKey())
					{
					case PropertyKey.Key_SampleType:
						SampleType = (SampleType)stream.ReadInt();
						break;
					case PropertyKey.Key_TargetName:
						TargetName = stream.ReadString();
						break;
					case PropertyKey.Key_LeftValue:
						LeftValue = stream.ReadDouble();
						break;
					case PropertyKey.Key_LeftOperator:
						LeftOperator = (EOperator)stream.ReadInt();
						break;
					case PropertyKey.Key_CalcResult:
						CalcResult = (ECalcResult)stream.ReadInt();
						break;
					case PropertyKey.Key_RightOperator:
						RightOperator = (EOperator)stream.ReadInt();
						break;
					case PropertyKey.Key_RightValue:
						RightValue = stream.ReadDouble();
						break;
					case PropertyKey.Key_Content:
						Content = stream.ReadString();
						break;
					case PropertyKey.Key_Color:
						Color = (Color)ColorConverter.ConvertFromString(stream.ReadString());
						break;
					case PropertyKey.Key_IsAnyTarget:
						IsAnyTarget = stream.ReadBool();
						break;
					default:
						stream.Seek(-4L, SeekOrigin.Current);
						return true;
					}
				}
			}
			stream.Seek(-4L, SeekOrigin.Current);
			return false;
		}
	}
}
